package com.chenyue.cm.user.controller;


import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.user.domain.JdThirdApply;
import com.chenyue.cm.user.domain.JdThirdApplyExample;
import com.chenyue.cm.user.domain.JdThirdInformation;
import com.chenyue.cm.user.service.JdAppUserRoleService;
import com.chenyue.cm.user.service.JdThirdApplyService;
import com.chenyue.cm.user.service.JdVerifyService;
import com.chenyue.cm.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdThirdApply")
public class JdThirdApplyController {

    private final JdThirdApplyService jdThirdApplyService;

    private final JdAppUserRoleService appUserRoleService;

    private final JdVerifyService verifyService;

    public JdThirdApplyController(JdThirdApplyService jdThirdApplyService, JdAppUserRoleService appUserRoleService, JdVerifyService verifyService) {
        this.jdThirdApplyService = jdThirdApplyService;
        this.appUserRoleService = appUserRoleService;
        this.verifyService = verifyService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdThirdApply> result = jdThirdApplyService.getPageBean(pageInfo, JdThirdApply.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdThirdApply vo) {
        boolean success = jdThirdApplyService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo 修改实体
     * @param request id
     *
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdThirdApply vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdThirdApplyService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdThirdApplyExample aExample = new JdThirdApplyExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdThirdApplyService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdThirdApplyService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

    @RequestMapping("/applyCheck")
    public Success<String> applyCheck(
            Integer state, Integer applyId, String refuseReason
    ) {
        JdThirdApply entity = new JdThirdApply();
        entity.setId(applyId);
        entity.setCheckState(state);
        entity.setRefuseReason(refuseReason);
        jdThirdApplyService.updateById(entity);
        Integer userId = (jdThirdApplyService.getById(applyId) != null) ? jdThirdApplyService.getById(applyId).getUserId(): null;
        if (state == 1) {
            appUserRoleService.setRole(userId, 2);
            JdThirdInformation information = new JdThirdInformation();
            information.setUserId(userId);
            information.setCt(DateUtils.getToday());
            information.setName(verifyService.getVerifyInfo(userId).getName());
            jdThirdApplyService.editThirdInfo(information);
        }
        verifyService.checkVerify(userId, state, refuseReason);
        return Success.success();
    }

}
